<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\model\Admin;
use app\BaseController;
use think\facade\Cookie;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;

class User extends BaseController
{
    public function user()
    {
        $user = Admin::getAdmin();
        View::assign('user',$user);
        return view();
    }

    public function userAdd()
    {
        return view();
    }

    public function userEdit()
    {
        return view();
    }

    // 初始化
    protected function initialize()
    {

    }

    //登录
    public function login()
    {
        if(Request::isPost()){
            $post = Request::post();
            $code = Request::post('code');
            //try{
            //    validate(['code'=>'require|captcha'])->check(['code'=>$code]);
            //}catch (ValidateException $e){
            //    return $e->getMessage();
            //}
            if(Request::Post('code')) {
                if (!captcha_check($code)) {
                    $this->error('验证码错误');
                }
            }
            $pwd = md5($post['pwd']);
            //$log = Db::table('ecm_admin')->where("name='{$post['name']}' and pwd='{$pwd}'")->find(1);
            $where = [];
            $where[] = ['name','=',$post['name']];
            $where[] = ['pwd','=',$pwd];

            //$login = Admin::getOne($where);
            $log = Admin::getOne($where);
            if(!$log){
                $num=Cookie::get('num')+1;
                if($num<5) {
                    Cookie::set('num',"{$num}", 3600);
                }else{
                    Cookie::set('num','5',86400);
                }
                $this->error('账户或者密码错误');
                //echo "<script>alert('账户或者密码错误');history.back()</script>";
            }else{
                Cookie::delete('num');
                Session::set('login',$log);
                $this->success('登陆成功',url('Index/index'));
            }
        }
        $num=Cookie::get('num');
        View::assign('num',$num);
        return \view();
    }

}